package com.educenter.controller;

import com.educenter.utils.ConstantWxUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

@CrossOrigin
@Controller
@RequestMapping("/api/ucenter/wx")
public class WxApiController {
	//生成微信二维码
	@GetMapping("/login")
	public String getWxCode(){
		//法一：直接地址拼接
//		String url = "https://open.weixin.qq.com/connect/qrconnect?appid="+ ConstantWxUtils.WX_OPEN_APP_ID +"&redirect_uri=";

		//法二：
		String baseUrl = "https://open.weixin.qq.com/connect/qrconnect" +
				"?appid=%s" +
				"&redirect_uri=%s" +
				"&response_type=code" +
				"&scope=snsapi_login" +
				"&state=%s" +
				"#wechat_redirect";

		String redirectUrl = ConstantWxUtils.WX_OPEN_REDIRECT_URL;
		try {
			URLEncoder.encode(redirectUrl,"utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}

		String url = String.format(
				baseUrl,
				ConstantWxUtils.WX_OPEN_APP_ID,
				ConstantWxUtils.WX_OPEN_REDIRECT_URL,
				"online"
		);
		//重定向
		return "redirect:"+ url;
	}
}
